using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Production
{
    [Authorize]
    public class PlanOrderTypeAppService : BasicArchivesAppService, IPlanOrderTypeAppService
    {
        protected IPlanOrderTypeRepository PlanOrderTypeRepository => LazyServiceProvider.LazyGetRequiredService<IPlanOrderTypeRepository>();
        protected PlanOrderTypeManager PlanOrderTypeManager => LazyServiceProvider.LazyGetRequiredService<PlanOrderTypeManager>();

        [Authorize(BasicArchivesPermissions.PlanOrderType.Query)]
        public async Task<PlanOrderTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<PlanOrderType, PlanOrderTypeDto>(
                await PlanOrderTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Query)]
        public async Task<PlanOrderTypeDto> FindAsync(string filter)
        {
            var entity = await PlanOrderTypeRepository.FindAsync(x => x.DisplayName == filter);
            return ObjectMapper.Map<PlanOrderType, PlanOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Query)]
        public async Task<PlanOrderTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await PlanOrderTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<PlanOrderType, PlanOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Query)]
        public async Task<PagedResultDto<PlanOrderTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await PlanOrderTypeRepository.GetCountAsync(input.Filter, true);
            var list = await PlanOrderTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<PlanOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<PlanOrderType>, List<PlanOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Query)]
        public async Task<PagedResultDto<PlanOrderTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<PlanOrderType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<PlanOrderType>(input.FilterGroup);
            }

            var totalCount = await PlanOrderTypeRepository.GetCountAsync(expression, true);
            var list = await PlanOrderTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<PlanOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<PlanOrderType>, List<PlanOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Create)]
        public async Task<PlanOrderTypeDto> CreateAsync(PlanOrderTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<PlanOrderTypeCreateDto, PlanOrderType>(input);

            await PlanOrderTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Update)]
        public async Task<PlanOrderTypeDto> UpdateAsync(Guid id, PlanOrderTypeUpdateDto input)
        {
            var entity = await PlanOrderTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await PlanOrderTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.PlanOrderType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await PlanOrderTypeManager.DeleteAsync(id);
        }
    }
}